﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DatabaseCore;
using SystemCore;
using System.Transactions;

namespace EakTest
{
    public partial class UserInquiry : MasterForm
    {
        IUserDao userDao = new UserDao();
        IRoleDao roleDao = new RoleDao();
        private IList<tbl_User> DataSource { get; set; }

        public UserInquiry()
        {
            InitializeComponent();

            Enter_ScreenMode(ScreenMode.Idle);

            BindComboBox();
            BindData();
        }

        private void BindComboBox()
        {
            var roleList = roleDao.GetAllRole();

            DataGridViewComboBoxColumn col = gvUser.Columns[(int)EUserCol.Role] as DataGridViewComboBoxColumn;
            col.DataSource = roleList;
            col.DisplayMember = "Name";
            col.ValueMember = "Id";

            ddlRole.DataSource = roleList;
            ddlRole.DisplayMember = "Name";
            ddlRole.ValueMember = "Id";
        }

        private void BindData()
        {
            ClearGridView();
            DataSource = userDao.GetAllUser();

            int i = 0;
            foreach (var item in DataSource)
            {
                gvUser.Rows.Add();
                gvUser[(int)EUserCol.Id, i].Value = item.Id;
                gvUser[(int)EUserCol.UserName, i].Value = item.UserName;
                gvUser[(int)EUserCol.Email, i].Value = item.Email;
                gvUser[(int)EUserCol.FullName, i].Value = item.FullName;
                gvUser[(int)EUserCol.Role, i].Value = item.Role;
                gvUser[(int)EUserCol.Tel, i].Value = item.Telephone;
                gvUser[(int)EUserCol.Fax, i].Value = item.Fax;
                gvUser[(int)EUserCol.Status, i].Value = item.Active;

                i++;
            }
        }

        private void ClearGridView()
        {
            gvUser.Rows.Clear();
            gvUser.DataSource = null;
        }

        #region Enter Mode

        public override void Enter_IdleMode()
        {
            base.Enter_IdleMode();
            ClearData();
        }

        public override void Enter_ViewMode()
        {
            base.Enter_ViewMode();
        }

        public override void Enter_AddMode()
        {
            base.Enter_AddMode();
            ClearData();
        }

        public override void Enter_EditMode()
        {
            base.Enter_EditMode();
        }

        public override void Enter_DeleteMode()
        {
            base.Enter_DeleteMode();
        }

        #endregion

        #region Save Mode

        public override bool Save_AddMode()
        {
            var userName = txtUser.Text.Trim();
            var email = txtEmail.Text.Trim();
            var displayName = txtDisplayName.Text.Trim();
            var role = ddlRole.SelectedValue.ToString();
            var tel = txtTel.Text.Trim();
            var fax = txtFax.Text.Trim();
            var active = chkStatus.Checked;

            #region validate
            if (string.IsNullOrEmpty(userName))
            {
                MessageDialog.Information("XXX");
                return false;
            }

            if (string.IsNullOrEmpty(email))
            {
                MessageDialog.Information("XXX");
                return false;
            }

            if (string.IsNullOrEmpty(displayName))
            {
                MessageDialog.Information("XXX");
                return false;
            }

            if (string.IsNullOrEmpty(role))
            {
                MessageDialog.Information("XXX");
                return false;
            }
            #endregion

            try
            {
                using (var ts = new TransactionScope())
                {
                    tbl_User dto = new tbl_User();
                    dto.UserName = userName;
                    dto.Password = "1234";
                    dto.Email = email;
                    dto.FullName = displayName;
                    dto.Role = Convert.ToInt32(role);
                    dto.Telephone = tel;
                    dto.Fax = fax;
                    dto.Active = active;

                    userDao.Insert(dto);

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                MessageDialog.Error(ex.Message);
                return false;
            }

            BindData();
            return true;
        }
        public override bool Save_EditMode()
        {
            var id = Convert.ToInt32(gvUser[(int)EUserCol.Id, gvUser.CurrentCell.RowIndex].Value);
            var userName = txtUser.Text.Trim();
            var email = txtEmail.Text.Trim();
            var displayName = txtDisplayName.Text.Trim();
            var role = ddlRole.SelectedValue.ToString();
            var tel = txtTel.Text.Trim();
            var fax = txtFax.Text.Trim();
            var active = chkStatus.Checked;

            #region validate
            if (string.IsNullOrEmpty(userName))
            {
                MessageDialog.Information("XXX");
                return false;
            }

            if (string.IsNullOrEmpty(email))
            {
                MessageDialog.Information("XXX");
                return false;
            }

            if (string.IsNullOrEmpty(displayName))
            {
                MessageDialog.Information("XXX");
                return false;
            }

            if (string.IsNullOrEmpty(role))
            {
                MessageDialog.Information("XXX");
                return false;
            }
            #endregion

            try
            {
                using (var ts = new TransactionScope())
                {
                    tbl_User dto = new tbl_User();
                    dto.Id = id;
                    //dto.UserName = userName;
                    //dto.Email = email;
                    dto.FullName = displayName;
                    dto.Role = Convert.ToInt32(role);
                    dto.Telephone = tel;
                    dto.Fax = fax;
                    dto.Active = active;

                    userDao.Update(dto);

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                MessageDialog.Error(ex.Message);
                return false;
            }

            BindData();
            return true;
        }

        public override bool Save_DeleteMode()
        {
            try
            {
                using (var ts = new TransactionScope())
                {
                    int id = Convert.ToInt32(gvUser[(int)EUserCol.Id, gvUser.CurrentCell.RowIndex].Value);
                    userDao.Delete(id);

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                MessageDialog.Error(ex.Message);
                return false;
            }

            BindData();
            return true;
        }

        #endregion

        #region Enabled Control

        public override void EnableButton_IdleMode()
        {
            base.EnableButton_IdleMode();
            ControlUtility.EnabledControl(false, txtUser, txtEmail, txtDisplayName, ddlRole, txtTel, txtFax, chkStatus);
            ControlUtility.EnabledControl(true, gvUser);
        }

        public override void EnableButton_ViewMode()
        {
            base.EnableButton_ViewMode();
            ControlUtility.EnabledControl(false, txtUser, txtEmail, txtDisplayName, ddlRole, txtTel, txtFax, chkStatus);
            ControlUtility.EnabledControl(true, gvUser);
        }

        public override void EnableButton_AddMode()
        {
            base.EnableButton_AddMode();
            ControlUtility.EnabledControl(true, txtUser, txtEmail, txtDisplayName, ddlRole, txtTel, txtFax, chkStatus);
            ControlUtility.EnabledControl(false, gvUser);
        }

        public override void EnableButton_EditMode()
        {
            base.EnableButton_EditMode();
            ControlUtility.EnabledControl(false, txtUser, txtEmail);
            ControlUtility.EnabledControl(true, txtDisplayName, ddlRole, txtTel, txtFax, chkStatus);
            ControlUtility.EnabledControl(false, gvUser);
        }

        #endregion

        private void gvUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Enter_ScreenMode(ScreenMode.View);

            MappingGridToScreen(e.RowIndex);
        }

        private void MappingGridToScreen(int iRow)
        {
            if (iRow != -1)
            {
                txtUser.Text = gvUser[(int) EUserCol.UserName, iRow].Value.ToString();
                txtEmail.Text = gvUser[(int) EUserCol.Email, iRow].Value.ToString();
                txtDisplayName.Text = gvUser[(int) EUserCol.FullName, iRow].Value.ToString();
                ddlRole.SelectedValue = gvUser[(int) EUserCol.Role, iRow].Value;
                txtTel.Text = gvUser[(int)EUserCol.Tel, iRow].Value == null ? string.Empty : gvUser[(int)EUserCol.Tel, iRow].Value.ToString();
                txtFax.Text = gvUser[(int)EUserCol.Fax, iRow].Value == null ? string.Empty : gvUser[(int)EUserCol.Fax, iRow].Value.ToString();
                chkStatus.Checked = Convert.ToBoolean(gvUser[(int) EUserCol.Status, iRow].Value);
            }
        }

        private void ClearData()
        {
            ControlUtility.ClearControlData(txtUser, txtEmail, txtDisplayName, ddlRole, txtTel, txtFax, chkStatus);
        }
    }
}
